﻿using System.Collections.Generic;
using System.Linq;
using WI.CandidateManager.Common.Data.Entities;
using WI.CandidateManager.Common.Data.Enums;

namespace WI.CandidateManager.Business.Objects
{
    /// <summary>
    /// Admin business logic.
    /// </summary>
    public class AdminBO : UserBaseBO
    {
        /// <summary>
        /// Creates new admin in database.
        /// </summary>
        /// <param name="admin">
        /// The admin.
        /// </param>
        public void CreateAdmin(Admin admin)
        {
            admin = (Admin)PrepareUser(admin);
            admin.Role = UserRole.Admin;

            DataContext.AdminSet.Add(admin);
            DataContext.SaveChanges();
        }

        /// <summary>
        /// Gets admin by ID.
        /// </summary>
        /// <param name="id">
        /// The ID.
        /// </param>
        /// <returns>
        /// The WI.CandidateManager.Common.Data.Entities.Admin.
        /// </returns>
        public Admin GetAdminById(long id)
        {
            return DataContext.AdminSet.Single(a => a.Id == id);
        }

        /// <summary>
        /// Gets a list of admins.
        /// </summary>
        /// <returns>
        /// The System.Collections.Generic.List`1[T -&gt; WI.CandidateManager.Common.Data.Entities.Admin].
        /// </returns>
        public List<Admin> GetAdminsList()
        {
            return DataContext.AdminSet.Where(a => a.State == State.Active).ToList();
        }

        /// <summary>
        /// Updates admin record on specified in entity ID;
        /// </summary>
        /// <param name="admin">
        /// The admin.
        /// </param>
        public void UpdateAdmin(Admin admin)
        {
            var oldAdmin = DataContext.AdminSet.Single(a => a.Id == admin.Id);

            oldAdmin.FullName = admin.FullName;
            oldAdmin.ShortName = admin.ShortName;
            oldAdmin.Email = admin.Email;
            oldAdmin.Phone = admin.Phone;
            oldAdmin.Gender = admin.Gender;

            DataContext.SaveChanges();
        }
    }
}
